﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using DAL;
using BLL;
public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


   
    protected void UserRegister(object sender, EventArgs e)
    {
        try
        {
            PersonalInformation info = new PersonalInformation();
            info.Name = this.Name.Text;
            info.Nickname = this.Nickname.Text;
            info.Password = this.Password.Text;
            info.Phone = this.Phone.Text;
            info.Email = this.Email.Text;
            if (this.GenderRadio.SelectedIndex == 0)
                info.Gender = 1;
            else
            {
                info.Gender = 0;
            }

            PersonalInformation newInfo = PersonalInformationManage.AddPersonalInformation(info);
            if (newInfo != null)
            {
                // 添加成功
                // 添加到房主或客户
                if (this.TypeRadio.SelectedIndex == 0)
                {
                    // 添加房主
                    HouseHolder hHolder = new HouseHolder();
                    hHolder.PersonalInformationId = newInfo.Id;
                    // 默认信息
                    hHolder.IsChecked = 0;
                    hHolder.IsRecommended = 0;
                    hHolder.EvaluationScore = 0;

                    HouseHolderManage.AddHouseHolder(hHolder);

                    // 写session
                    Session.Add("UserType", 0);
                    Session.Add("UserId", hHolder.Id);
                    // 跳转到个人页面
                    Response.Redirect("/Home.aspx");
                }
                else
                {
                    // 添加客户
                    Client client = new Client();
                    client.PersonalInformationId = newInfo.Id;
                    ClientManage.AddClient(client);

                    // 写session
                    Session.Add("UserType", 1);
                    Session.Add("UserId", client.Id);
                    // 跳转到个人页面
                    Response.Redirect("/Home.aspx");
                }
                Response.Write("Success");
                // 跳转到添加成功页面
                
            }
            else
            {
                // 添加失败
                // 显示添加失败
                Response.Write("Failure");
            }
        
         
        }
        catch(Exception ep)
        {
            Console.WriteLine(ep.Message);
        }
    }
}